/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.diamondcolor;

import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.model.master.DiamondColor;
import com.kmcsoft.spring.SpringUtil;
import com.kmcsoft.utils.i18n;
import java.util.ArrayList;
import java.util.Collection;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author huyvu
 */
public class DiamondColorComposer extends GenericForwardComposer {

    private ShopGenericService service;
    private AnnotateDataBinder binder;
    private AnnotateDataBinder paBinder;
    private Component component;
    private Collection<DiamondColor> colors;
    private Collection<DiamondColor> deleteColors;

    //<editor-fold defaultstate="collapsed" desc="GET/SET">
    public Collection<DiamondColor> getColors() {
        if (colors.isEmpty()) {
            colors.add(new DiamondColor());
        }
        return colors;
    }

    public void setColors(Collection<DiamondColor> colors) {
        this.colors = colors;
    }

    private AnnotateDataBinder getBinder() {
        return binder;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterCompose">
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        if (service == null) {
            service = SpringUtil.getBean("shopGenericService", ShopGenericService.class);
        }
        self.setAttribute("comp", this);
        component = (Component) arg.get("component");
        paBinder = (AnnotateDataBinder) arg.get("binder");
        binder = new AnnotateDataBinder(self);
        colors = service.getDiamondColors();
        deleteColors = new ArrayList<DiamondColor>();
        self.setAttribute("binder", binder);
        binder.loadAll();

    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onInsertRow">
    public void onInsertRow(ForwardEvent event) {
        Grid grid = ((Grid) event.getOrigin().getTarget().getParent().getParent().getParent());
        colors.add(new DiamondColor());
        getBinder().loadComponent(grid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onRemove">
    public void onRemove(ForwardEvent event) {
        Grid grid = (Grid) event.getOrigin().getTarget().getParent().getParent().getParent();
        if (grid.getModel().getSize() == 1) {
            return;
        }
        Toolbarbutton btton = (Toolbarbutton) event.getOrigin().getTarget();
        Row row = ((Row) btton.getParent());
        DiamondColor selectedRecord = (DiamondColor) row.getValue();
        if (selectedRecord.getId() != 0) {
            if (service.checkUsingDiamondColor(selectedRecord)) {
                throw new WrongValueException(i18n.l("shop.diamondColor.error.delete"));
            }
            deleteColors.add(selectedRecord);
        }
        ((BindingListModelList) grid.getModel()).remove(selectedRecord);
        grid.renderAll();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onCancelModal">
    public void onCancelModal() {
        self.detach();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onClick$save">
    public void onClick$save() {
        checkBeforeSave();
        service.makePersistentAll(colors);
        for (DiamondColor color : deleteColors) {
            service.deletePersistent(color);
        }
        paBinder.loadComponent(component);
        self.detach();

    }
    //</editor-fold>

    private void checkBeforeSave() {
        for (DiamondColor color : colors) {
            String name = color.getName().trim();
            boolean checkName = false;
            for (DiamondColor clor : colors) {
                if (clor.getName().trim().equalsIgnoreCase(name)) {
                    if (checkName) {
                        throw new WrongValueException(i18n.l("shop.diamondColor.error.duplicateName"));
                    }
                    checkName = true;
                }
            }
        }
    }
}
